package com.sen.paydemo.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.paydemo.entity.OrderInfo;
import com.sen.paydemo.entity.RefundInfo;
import com.sen.paydemo.mapper.RefundInfoMapper;
import com.sen.paydemo.service.OrderInfoService;
import com.sen.paydemo.service.RefundInfoService;
import com.sen.paydemo.util.OrderNoUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 退款信息Service实现
 *
 * @author 韩宗森
 * @since 2023-12-29 17:17:20
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

@Resource
private OrderInfoService orderInfoService;

    @Override
    public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
        // 根据订单号获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);

        // 根据订单号生成退款订单
        RefundInfo refundInfo = new RefundInfo();
        refundInfo.setOrderNo(orderNo); // 订单编号
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo());//退款单编号
        refundInfo.setTotalFee(orderInfo.getTotalFee());// 原订单金额(分)
        refundInfo.setRefund(orderInfo.getTotalFee());//退款金额(分)
        refundInfo.setReason(reason);

        // 保存退款订单
        baseMapper.insert(refundInfo);
        return refundInfo;
    }

    // 记录退款记录
    @Override
    public void updateRefund(String content) {
        // json字符串转map
        Map<String,String> resultMap = JSON.parseObject(content, HashMap.class);

        // 根据退款单编号修改退款单
        QueryWrapper<RefundInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("refund_no",resultMap.get("out_refund_no"));

        //设置要修改的字段
        RefundInfo refundInfo = new RefundInfo();

        refundInfo.setRefundId(resultMap.get("refund_id"));// 微信支付退款单号

        //查询退款和申请退款中的返回参数
        if (resultMap.get("status") != null){
            refundInfo.setRefundStatus(resultMap.get("status"));// 退款状态
            refundInfo.setContentReturn(content);// 将全部响应结果存入数据库的content字段
        }

        // 退款回调中的回调参数
        if (resultMap.get("refund_status") != null){
            refundInfo.setRefundStatus(resultMap.get("refund_status"));// 退款状态
            refundInfo.setContentNotify(content);// 将全部响应结果存入数据库
        }

        //更新退款单
        baseMapper.update(refundInfo,queryWrapper);
    }
}
